package com.cs456.TeamBobcat;

import java.io.IOException;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.media.ExifInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.cs456.TeamBobcat.database.Constants;
import com.cs456.TeamBobcat.database.MyDB;
import com.cs456.TeamBobcat.views.StaticMapLayout;

public class WhereAmIResult extends Activity {
	StaticMapLayout _map;
	Button _homeButton;
	MyDB dba;
	int imageSize;
	protected ArrayList <String> _pathBag;

	double[] GPS;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.whereamiresult);
		dba = new MyDB(this);
        dba.open();
        

		initGPSMap();
		initHomeButton();
		
		
	}

	private void initHomeButton() {
		_homeButton = (Button) findViewById(R.id.WAIResult_Home);
		_homeButton.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				finish();
			}
		});
	}

	private void initGPSMap() {
		_map = (StaticMapLayout) findViewById(R.id.WAIResult_Text);
		_map.setMap(R.drawable.map2, 39.343989f, -82.120829f, 39.312851f, -82.079802f);


		Intent intent = getIntent();
		GPS = intent.getDoubleArrayExtra(WebService.WHERE_AM_I_RESULT_KEY);
		
//		_map.setText("Lattitude: " + GPS[0] + "\n Longitude: " + GPS[1]);
		_map.setCurrentLocation((float)GPS[0], (float)GPS[1]);
		
		Cursor prevLocs = dba.getgps();
		prevLocs.moveToFirst();
        while (prevLocs.isAfterLast() == false) {
        	int latIndex = prevLocs.getColumnIndex(Constants.GPS_X);
        	int longIndex = prevLocs.getColumnIndex(Constants.GPS_Y);
        	
        	float lat = prevLocs.getFloat(latIndex);
        	float lon = prevLocs.getFloat(longIndex);
            _map.addPastLocation(lat, lon);
            prevLocs.moveToNext();
        }		
		
		dba.insertgps(GPS[0], GPS[1]);
		_pathBag = intent.getStringArrayListExtra("pics");
		//Save the GPS location into .JEPG files
		for (int i=0; i<_pathBag.size(); i++){
			try{
				ExifInterface exif = new ExifInterface(_pathBag.get(i));
				exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE, Double.toString(GPS[0]));
				exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE, Double.toString(GPS[1]));
			}catch(IOException e){}
			
		}

	}

}
